home *** CD-ROM | disk | FTP | other *** search
- // Cookies v1.56 (2/7/98)
- //
- // Inserts a random quote into an Ovation Pro Document
- // (this time with desktop interface)
- //
- // Tony Howat, (c) RISC User 1998
-
- int cookie_rnd_seed=1,cookie_count=0;
- string cookie_filename;
-
- int cookie_handle;
- int cookie_status = 0;
- int cookie_chosen=0;
-
- int cookie_rnd(void)
- {
- int result;
- cookie_rnd_seed=(cookie_rnd_seed*1103515245)+12345;
- result=cookie_rnd_seed >> 16;
- if(result<0)
- result=0-result; /* get rid of any negative results */
- return(result);
- }
-
- int cookie_count_cookies(void)
- {
- int file;
- string error;
-
- file=fileopen(cookie_filename,"r");
-
- if(!file)
- {
- error="{COOKIES_NOFILE}"+" ("+cookie_filename+")";
- errorbox(error);
- } else {
- // now scan through the file looking for nulls
-
- while(!fileeof(file))
- {
- if(filegetc(file)==0)
- ++cookie_count;
- }
-
- fileclose(file);
- }
- }
-
- int cookies_message(int write)
- {
- int file, firstchar=0xff, c=0, dispcharcount=0;
- string error, displaycookie, writecookie;
-
- file=fileopen(cookie_filename,"r");
-
- if(!file)
- {
- error="{COOKIES_NOFILE}"+" ("+cookie_filename+")";
- errorbox(error);
- return 0;
- }
-
- if(cookie_count)
- {
- if(!write)
- {
- cookie_rnd_seed = (clock() * 4267) & 0xffffffff;
- cookie_chosen=cookie_rnd();
- cookie_chosen=cookie_chosen % cookie_count;
- c=0;
- }
-
- // now scan through the file looking for our cookie
-
- while((!fileeof(file)) && (c!=cookie_chosen))
- {
- if(filegetc(file)==0)
- ++c;
- }
-
- if(cookie_chosen!=0)
- filereads(displaycookie,file); // skip leading newline
-
- displaycookie="";
- writecookie="";
-
- firstchar=10;
-
- while(firstchar!=0)
- {
- firstchar=filegetc(file);
- if(firstchar!=0)
- {
- string s;
- s=chars(firstchar);
- if(write)
- writecookie+=s;
- if(firstchar!=10 && s!="\t" && dispcharcount<255)
- {
- displaycookie+=s;
- dispcharcount++;
- } else {
- if((firstchar==10 || s=="\t") && dispcharcount<255)
- {
- displaycookie+=" ";
- dispcharcount++;
- }
- }
- }
- }
-
- // add dots if our length was greater than our (self imposed) icon limit
- if(dispcharcount>=255)
- {
- displaycookie=mids(displaycookie,0,252);
- displaycookie+="...";
- }
-
- if(write)
- type(writecookie);
- writeicon(cookie_handle, 0, "");
- refresh_icon(0, cookie_handle, 0);
- writeicon(cookie_handle, 0, displaycookie);
- }
-
- fileclose(file);
-
- return(0);
- }
-
- // deal with 'cookies' menu entry
-
- int cookies_entry(int entry, int subcode)
- {
- cookie_status = !cookie_status;
- if(cookie_status)
- {
- display_window(cookie_handle, 0, 1);
- cookies_message(0);
- }
- else
- close_window(cookie_handle);
-
- return(0);
- }
-
- // tick or un-tick 'cookies' menu entry
-
- int cookies_flags(int entry, string &text)
- {
- return(cookie_status);
- }
-
- // handler for mouse clicks in cookies box
-
- void cookies_clicknext(int handle,int icon,int bits,int mx,int my)
- {
- if(icon == 4)
- cookies_message(0);
- if(icon == 5)
- cookies_message(1);
- }
-
- // handler for cookies window close icon
-
- void cookies_closebox(int handle)
- {
- close_window(handle);
- cookie_status = 0;
- }
-
- // add our option to the applet menu
-
- void main(void)
- {
- script_menu_initialise();
-
- addentry_menu(script_handle,"cookies_entry","cookies_flags","","","{COOKIES_00}");
-
- cookie_handle = create_window("cook_main");
- addwindowhandler(0,cookie_handle,"cookies_clicknext");
- addwindowhandler(2,cookie_handle,"cookies_closebox");
-
- addeventhandler(0x004, 0, "cookies_quit");
-
- cookie_filename="OvationPro$AppletsDir";
- getenvs(cookie_filename);
- cookie_filename+=".!Cookies.AResources.Cookies";
-
- cookie_count_cookies();
-
- cookie_rnd_seed = (clock() * 4267) & 0xffffffff;
- cookie_chosen=cookie_rnd();
- cookie_chosen=cookie_chosen % cookie_count;
- }
-